// pages/orderInfo/orderInfo.js
var app = getApp();
var host = app.globalData.host;
var self = null;

Page({
  data: {
    themeColor: '',
    orderId: null,
    info: null
  },
  onLoad: function(options) {
    self = this;
    self.getStyle();
    self.setData({
      orderId: options.orderId
    })
  },
  onShow: function() {
    self.init();
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor
    })
  },
  init() {
    var url = host + '/yq/order/info';
    var params = {
      orderId: self.data.orderId
    };
    var success = function(res) {
      console.log(res);
      self.setData({
        info: res.info
      })
    };
    var fail = function(res) {};
    var complete = function() {};
    app.request(url, params, success, fail, complete);
  },
  goProductInfo(e) {
    var productId = self.data.info.productId
    var itemId = self.data.info.itemId
    if (itemId) {
      wx.navigateTo({
        url: '/pages/productInfo/productInfo?itemId=' + itemId
      })
    } else if (productId) {
      wx.navigateTo({
        url: '/pages/productInfo/productInfo?productId=' + self.data.info.productId
      })

    }
  },
  goPay(e) {
    console.log(self.data.orderId);
    wx.showLoading({
      title: '提交中',
    })
    var url = host + '/yq/order/pay';
    var data = {
      orderId: self.data.orderId
    };
    var success = function (res) {
      console.log(res);
      wx.hideLoading()
      var status = res.status
      if (status == 204) {
        wx.showModal({
          title: '出错了',
          content: res.msg
        })
        return;
      }
      var response = res.info
      var nonce_str = response.nonce_str
      var prepay_id = response.prepay_id
      var sign = response.sign
      var timestamp = response.timeStamp
      var appId = response.appid
      var param = {
        'appId': appId,
        'timeStamp': '' + timestamp,
        'nonceStr': '' + nonce_str,
        'package': 'prepay_id=' + prepay_id,
        'signType': 'MD5',
        'paySign': '' + sign,
        'success': function (res) {
          setTimeout(function () {
            wx.navigateTo({
              url: '/pages/success/success?isShowContent=' + 1
            })
          }, 200)

        },
        'fail': function (res) {
          wx.showModal({
            title: '出错了',
            content: '支付失败'
          })
        }
      }
      wx.requestPayment(param);
    };
    var fail = function (res) { };
    var complete = function () { };
    app.request(url, data, success, fail, complete);
  }
})